#include <iostream>
#include <fstream>
#include <string>
#include "contacts.pb.h"

void AddPeopleInfo(contacts2::PeopleInfo *people)
{
    std::cout << "--------------------新增联系人--------------------" << std::endl;
    std::cout << "请输入联系人的姓名: ";
    std::string name;
    std::getline(std::cin, name);
    people->set_name(name);

    std::cout << "请输入联系人的年龄: ";
    int age;
    std::cin >> age;
    people->set_age(age);
    std::cin.ignore(256, '\n');

    for (int i = 0;; i++)
    {
        std::cout << "请输入联系人的电话 " << i + 1 << " " << "(只输⼊回⻋完成电话新增): ";
        std::string number;
        std::getline(std::cin, number);
        if (number.empty())
            break;

        contacts2::PeopleInfo_Phone *phone = people->add_phone();
        phone->set_number(number);

        std::cout << "请输入该电话的类型 (1. 移动电话   2. 固定电话): ";
        int type;
        std::cin >> type;
        std::cin.ignore(256, '\n');
        switch (type)
        {
        case 1:
            phone->set_type(contacts2::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
            break;
        case 2:
            phone->set_type(contacts2::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
            break;
        default:
            std::cout << "选择有误! " << std::endl;
            break;
        }
    }

    contacts2::Address address;
    std::cout << "请输入联系人家庭地址: " << std::endl;
    std::string home_address;
    std::getline(std::cin, home_address);
    address.set_home_address(home_address);
    std::cout << "请输入联系人的单位地址: " << std::endl;
    std::string unit_address;
    std::getline(std::cin, unit_address);
    address.set_unit_address(unit_address);

    // Address -> Any
    people->mutable_data()->PackFrom(address);


    std::cout << "请选择要添加的其他联系方式: (1. qq  2. wechat)";
    int other_contact;
    std::cin >> other_contact;
    std::cin.ignore(256, '\n');
    if(1 == other_contact) {
        std::cout << "请输入联系人的QQ号：";
        std::string qq;
        std::getline(std::cin, qq);
        people->set_qq(qq);
    } else if(2 == other_contact) {
        std::cout << "请输入联系的人 wechat：";
        std::string wechat;
        std::getline(std::cin, wechat);
        people->set_wechat(wechat);
    } else {
        std::cout << "选择有误" << std::endl;
    }

    for(int i = 0; ; i++) {
        std::cout << "请输入备注" << i + 1 << "标题：(只输入会车完成备注新增)";
        std::string remark_key;
        std::getline(std::cin, remark_key);
        if(remark_key.empty()) {
            break;
        }

        std::cout << "请输入备注内容: ";
        std::string remark_value;
        std::getline(std::cin, remark_value);
        people->mutable_remark()->insert({remark_key, remark_value});
    }


    std::cout << "-------------------添加联系人成功------------------" << std::endl;
}

int main()
{
    contacts2::Contacts contacts;
    // 读取本地已存在的通讯录文件
    std::fstream input("contacts.bin", std::ios::in | std::ios::binary);
    if (!input.is_open())
    {
        std::cout << "contacts.bin not exists, create new file" << std::endl;
    }
    else
    {
        if (!contacts.ParseFromIstream(&input))
        {
            std::cerr << "ParseFromIstream Failed!" << std::endl;
            input.close();
            return -1;
        }
    }

    // 向通讯录中添加一个联系人 返回开辟好空间的 peopleinfo对象
    AddPeopleInfo(contacts.add_contacts());

    // 将通讯录写入本地文件中
    std::fstream output("contacts.bin", std::ios::out | std::ios::trunc | std::ios::binary);
    if (!contacts.SerializeToOstream(&output))
    {
        std::cerr << "SerializeToOstream Failed!" << std::endl;
        input.close();
        output.close();
        return -1;
    }

    std::cout << "SerializeToOstream Success" << std::endl;

    input.close();
    output.close();

    return 0;
}